#include "StdAfx.h"
#include "BucketSort.h"

#include <iostream>


CBucketSort::CBucketSort(void)
{
	m_iMax = 0;
}


CBucketSort::~CBucketSort(void)
{
}

void CBucketSort::pushDataToPool(const int i)
{
	m_viPool.push_back(i);

	m_iMax = max(m_iMax, i);
}

void CBucketSort::eraseDataInPool(const int i)
{
	for (vector<int>::iterator it = m_viPool.begin(); it != m_viPool.end(); ++it)
	{
		m_viPool.erase(it);
		break;
	}

}

void CBucketSort::clearDataPool()
{
	m_viPool.clear();
}

void CBucketSort::DoSort()
{
	if (m_viPool.empty())
		return;

	m_vSortVector.clear();
	
	int *pTmp = new int[m_iMax+1];
	memset(pTmp, 0, m_iMax*sizeof(int));
	std::unique_ptr<int []> m_ptr(pTmp);
	
	int iTmp = 0;
	for (vector<int>::iterator it = m_viPool.begin(); it != m_viPool.end(); ++it)
	{
		(pTmp[*it])++;
	}

	for (int i = 0; i<m_iMax; i++)
	{
		if (0 != pTmp[i])
		{
			m_vSortVector.push_back(i);

			for (int x=0; x<pTmp[i]-1; x++)
			{
				m_vSortVector.push_back(i);
			}
		}
	}
	

	debugPrint();
}

int CBucketSort::GetList(list<int>& out_list)
{
	if (m_vSortVector.empty())
		return 0;

	out_list.clear();
	int icount = 0;
	for (vector<int>::iterator pPos = m_vSortVector.begin(); pPos != m_vSortVector.end(); ++pPos)
	{
		out_list.push_back(*pPos);
		icount++;
	}

	return icount;
}

void CBucketSort::debugPrint()
{
	for (vector<int>::iterator pPos = m_vSortVector.begin(); pPos != m_vSortVector.end(); ++pPos)
	{
		cout<<(*pPos)<<" ";
	}

	cout<<endl;
}